//jqeury檢測擴展
(function($){
	$.extend({
		//範圍
		inRange: function(val,param){
			var defaults = {		
				minlen: 0,						
				maxlen: 9999
			};
			param = $.extend(defaults,param);
			var length;
			switch($.type(val)){
				case "number":
					length = val;
					break;
				case "string": 
				case "array":
					length = val.length ? val.length : NaN;
					break;
				default:
					length = NaN;
					break;
			}
			return length>=param.minlen && length<=param.maxlen;
		},

		//數字
		isNumber: function(val,param){
			var defaults = {		
				float: false,						
				signed: false
			};
			param = $.extend(defaults,param);
			var pattern = param.signed ? (param.float ? /^-?\d+(\.{0,1}\d+){0,1}$/ : /^-?\d+$/) : (param.float ? /^\d+(\.{0,1}\d+){0,1}$/ : /^\d+$/);
			return pattern.test(val);
		},

		//字母
		isEn: function(val){
			var pattern = /^[A-Za-z]+$/;
			return pattern.test(val);
		},
		
		//中文
		isCn: function(val){
			var pattern = /^[\u4e00-\u9fa5]+$/i;
			return pattern.test(val);
		},

		//email
		isEmail: function(val){
			var pattern = /^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/;
			return pattern.test(val);
		},

		//日期
		isDate: function(val,param){
			var defaults = {		
				low: 1900,						
				high: 9999
			};
			param = $.extend(defaults,param);
			var pattern = /^[0-9]{4}-[0-9]{1,2}-[0-9]{1,2}$/;
			if(pattern.test(val)){
				var date = val.split('-');
				date[1] = Number(date[1]);
				date[2] = Number(date[2]);
				if(date[0] < param.low || date[0] > param.high){ return false; }	//年份限制
				var dt = new Date(date[0],date[1]-1,date[2]);
				date = date.join('-');
				var tdate = dt.getFullYear() + '-' + (dt.getMonth()+1) + '-' + dt.getDate();
				return tdate == date;
			}else{
				return false;
			}
		},
		
		//電話
		isPhone: function(val,param){
			var length = param > 0 ? param : 8;
			var pattern = new RegExp("^\\d{" + length + "}$");
			return pattern.test(val);
		},
		
		//身份證
		isIdcard: function(val,param){
			var defaults = {		
				letter: 2,						
				length: 4
			};
			param = $.extend(defaults,param);
			if(param.letter < 1 || param.letter > 2) param.letter = 2;
			if(param.length < 4 || param.length > 8) param.letter = 4;
			var exec = new Array();
			for(var i=1; i<=param.letter; i++){
				exec.push("(^[A-Za-z]{" + i + "}[0-9]{" + (param.length-i) + "}$)");
			}
			exec = exec.join('|');
			var pattern = new RegExp(exec);
			return pattern.test(val);
		},
		
		//網址
		isUrl: function(val){
			var pattern = /^(http(s)?:\/\/)?[a-zA-Z0-9\-_]+\.[a-zA-Z0-9\-_%\?\.=&;\\]+/;
			return pattern.test(val);
		},
		
		//文件類型
		checkFileExt: function(val,param){
			var ext = typeof(param)!='undefined'? param.toLowerCase() : 'jpg';
			var allext = ext.split('|');
			var fileext = val.substr(val.lastIndexOf('.')+1).toLowerCase();
			if($.inArray(fileext,allext)==-1){
				return false;
			}else{
				return true;
			}
		},

		//檢查val在對象中是否存在
		inObject: function(val,object){
			var isIn = false;
			if(typeof(object) == 'object'){
				$.each(object,function(k,v){
					if(val == v) isIn = true;
				});
			}
			return isIn;
		}
	});
})(jQuery);
